package com.ddwl.order.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.ddwl.common.annotation.Auth;
import com.ddwl.common.annotation.Submit;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.order.service.OrderAddressServer;
import com.ddwl.order.service.OrderInfoServer;
import com.ddwl.order.service.OrderPayServer;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.order.BargainActivityOrderBo;
import com.ddwl.schema.bo.order.CreateOrderBo;
import com.ddwl.schema.bo.order.OrderPayBo;
import com.ddwl.schema.bo.order.QueryOrderListBo;
import com.ddwl.schema.vo.base.*;
import com.ddwl.schema.vo.gate.kuaiDi100.QueryTrackVo;
import com.ddwl.schema.vo.order.*;
import com.ddwl.schema.vo.shop.ShopVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 */
@Api(tags = "用户订单操作相关接口")
@RestController
@RequestMapping("/user")
public class OrderInfoController extends BaseController {

    @Resource
    private OrderInfoServer orderInfoServer;
    @Resource
    private OrderPayServer orderPayServer;
    @Resource
    private OrderAddressServer orderAddresServer;

    @Submit
    @PostMapping("/create")
    @Auth(product={BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("创建用户订单")
    public RestVo<String> create(@Valid @RequestBody CreateOrderBo bo) {
        fillBaseField(bo);
        //客户端只能下普通订单
        bo.setOrderType(0);
        if(StrUtil.isEmpty(bo.getSource())&& !BaseEnum.ProductEnum.MALL.getName().equals(getProduct())){
            bo.setSource(getUserId());
        }
        return RestVo.ok(orderInfoServer.createOrderNew(bo));
    }

    @PostMapping("/bargainOrder/create")
    @Auth(product={BaseEnum.ProductEnum.API})
    public RestVo<String> createBargainOrder(@Valid @RequestBody BargainActivityOrderBo bo) {
        return RestVo.ok(orderInfoServer.createBargainOrder(bo));
    }

    @PostMapping("/page")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("查询我的订单")
    public RestVo<PageVo<OrderInfoListVo>> page(@Valid @RequestBody PageBo<QueryOrderListBo> pageBo) {
        pageBo.getParam().setUserId(getUserId());
        return RestVo.ok(orderInfoServer.getPage(pageBo));
    }

    @GetMapping("/detail")
    @ApiOperation("获取订单详情")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<OrderDetailVo> detail(@RequestParam String id) {
        return RestVo.ok(orderInfoServer.getDetail(id,getUserId()));
    }

    @GetMapping("/getMasterOrder")
    @ApiOperation("获取主订单详情")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<OrderInfoMasterVo> getMasterOrder(@RequestParam String id) {
        return RestVo.ok(orderInfoServer.getMasterOrder(id,getUserId()));
    }

    @GetMapping("/delivery/info")
    @ApiOperation("获取订单发货信息")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<OrderAddrVo> deliveryInfo(@RequestParam String orderId) {
        return RestVo.ok(orderAddresServer.getOrderAddr(orderId));
    }

    @GetMapping("/orderCount")
    @ApiOperation("获取单据数量数据")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL})
    public RestVo<UserOrderCountVo> getUserOrderCount() {
        return RestVo.ok(orderInfoServer.getUserOrderCount(getUserId()));
    }
    
    @GetMapping("/orderWbmCount")
    @ApiOperation("获取wbm后台单据数量数据")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<UserOrderCountVo> getWbmUserOrderCount() {
        return RestVo.ok(orderInfoServer.getWbmUserOrderCount(getUserId()));
    }

    @PostMapping("/orderWbmCount")
    @ApiOperation("获取wbm后台单据数量数据")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<UserOrderCountVo> getWbmUserOrderCount(@RequestParam String shopId,@RequestParam String openId) {
        return RestVo.ok(orderInfoServer.getWbmUserOrderCount(shopId,openId));
    }

    @DeleteMapping("/cancel")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("取消订单")
    public RestVo cancel(@RequestParam String id) {
        orderInfoServer.cancel(id, getUserId());
        return RestVo.ok();
    }

    @PostMapping("/receive")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("订单签收")
    public RestVo receive(@RequestParam String id) {
        orderInfoServer.receive(id, getUserId());
        return RestVo.ok();
    }

    @Submit
    @PostMapping("/prepay")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("订单发起支付")
    public RestVo<WxPayInfoVo> prepay(@RequestBody OrderPayBo bo) {
        fillBaseField(bo);
        return RestVo.ok(JSON.parseObject(orderPayServer.preWxPayNew(bo),WxPayInfoVo.class));
    }

    @Submit
    @PostMapping("/prepay1")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("订单发起支付")
    public RestVo<String> newPrepay(@RequestBody OrderPayBo bo) {
        fillBaseField(bo);
        return RestVo.ok(orderPayServer.preWxPayNew(bo));
    }


    @GetMapping("/orderInfo/getRepairTime")
    @Auth(product = {BaseEnum.ProductEnum.API})
    @ApiOperation("查询保修期限")
    public RestVo<Integer> getRepairTime() {
        return RestVo.ok(orderInfoServer.getRepairTime());
    }


    @GetMapping("/orderInfo/getOrderByPhone")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("根据电话号查询订单信息")
    public RestVo<List<OrderInfoVo>> getOrderByPhone(@RequestBody QueryOrderListBo bo) {
        ShopVo shopVo = getShopVo(bo.getShopId());
        bo.setShopId(shopVo.getId());
        return RestVo.ok(orderInfoServer.getOrderByPhone(bo));
    }

    @PostMapping("/unReceiveZtOrder")
    @ApiOperation("发送自提提醒短信")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo ztReceiveMsg(@RequestParam String orderId){
        orderInfoServer.ztReceiveMsg(orderId);
        return RestVo.ok();
    }

    @PostMapping("/unDealOrder")
    @ApiOperation("用户待处理订单信息")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<UserOrderDataVo> unDealOrder(){
        UserOrderDataVo vo=orderInfoServer.queryUnDealOrder(getUserId());
        return RestVo.ok(vo);
    }

    @GetMapping("/logistics/{id}")
    @ApiOperation("查询物流信息")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE, BaseEnum.ProductEnum.FENXIAO})
    public RestVo<QueryTrackVo> queryLogistics(@PathVariable("id") String id){
        return RestVo.ok(orderInfoServer.queryLogistics(id));
    }


}
